home *** CD-ROM | disk | FTP | other *** search
Text File | 1996-04-03 | 10.2 KB | 369 lines | [TEXT/MPS ] |
- //----------------------------------------------------------------------------------------
- // UDeskScrapView.cp
- // Copyright © 1985-96 by Apple Computer, Inc. All rights reserved.
- //----------------------------------------------------------------------------------------
-
- #ifndef __UDESKSCRAPVIEW__
- #include "UDeskScrapView.h"
- #endif
-
- // MacApp
-
- #ifndef __UCLIPBOARDMGR__
- #include "UClipboardMgr.h"
- #endif
-
- #ifndef __UCOREUTILITIES__
- #include "UCoreUtilities.h"
- #endif
-
- #ifndef __UDEBUG__
- #include "UDebug.h"
- #endif
-
- #ifndef __UGEOMETRY__
- #include "UGeometry.h"
- #endif
-
- #ifndef __UMACAPPGLOBALS__
- #include "UMacAppGlobals.h"
- #endif
-
- #ifndef __UMACAPPUTILITIES__
- #include "UMacAppUtilities.h"
- #endif
-
- #ifndef __UMEMORY__
- #include "UMemory.h"
- #endif
-
- // Toolbox
-
- #ifndef __ERRORS__
- #include <Errors.h>
- #endif
-
- // ANSI
-
- #ifndef __STDIO__
- #include <stdio.h>
- #endif
-
-
-
- //========================================================================================
- // static data members
- //========================================================================================
- Boolean TDeskScrapView::gVarClipPicSize;
-
- static Boolean LookForScrapType(ResType theResType, ResType& resTypeFound);
-
- //========================================================================================
- // CLASS TDeskScrapView
- //========================================================================================
- #undef Inherited
- #define Inherited TView
-
- #pragma segment MAInit
- MA_DEFINE_CLASS_M1(TDeskScrapView, Inherited);
-
- //----------------------------------------------------------------------------------------
- // TDeskScrapView constructor
- //----------------------------------------------------------------------------------------
- #pragma segment MAInit
-
- TDeskScrapView::TDeskScrapView()
- {
- fDataHandle = NULL;
- fHavePicture = FALSE;
- fHaveText = FALSE;
- fScrapCount = 0;
- } // TDeskScrapView::TDeskScrapView
-
- //----------------------------------------------------------------------------------------
- // TDeskScrapView::IDeskScrapView:
- //----------------------------------------------------------------------------------------
- #pragma segment MAInit
-
- void TDeskScrapView::IDeskScrapView()
- {
- VPoint itsSize(200, 200);
- PScrapStuff aPtr;
-
- // Can't know the extent required until we actually read the data in, which we won't bother
- // to do unless forced to by receiving an Update event
-
- this->IView(NULL, NULL, gZeroVPt, itsSize, sizeVariable, sizeVariable);
-
- aPtr = InfoScrap();
- fScrapCount = aPtr->scrapCount - 1;
- } // TDeskScrapView::IDeskScrapView
-
- //----------------------------------------------------------------------------------------
- // TDeskScrapView destructor
- //----------------------------------------------------------------------------------------
- #pragma segment MAClipboard
-
- TDeskScrapView::~TDeskScrapView()
- {
- if (this == gClipboardMgr->fClipOrphanage)
- gClipboardMgr->fClipOrphanage = NULL;
- }
-
- //----------------------------------------------------------------------------------------
- // TDeskScrapView::Free:
- //----------------------------------------------------------------------------------------
- #pragma segment MAClipboard
-
- void TDeskScrapView::Free() // override
- {
- // Never want to free gClipboardMgr->fClipOrphanage.
- if (this != gClipboardMgr->fClipOrphanage)
- Inherited::Free();
- } // TDeskScrapView::Free
-
- //----------------------------------------------------------------------------------------
- // TDeskScrapView::ReadFields:
- //----------------------------------------------------------------------------------------
- #pragma segment MAInit
-
- void TDeskScrapView::ReadFields(TStream* aStream)
- {
- PScrapStuff aPtr;
-
- Inherited::ReadFields(aStream);
-
- aPtr = InfoScrap();
- fScrapCount = (aPtr->scrapCount) - 1;
- } // TDeskScrapView::ReadFields
-
- //----------------------------------------------------------------------------------------
- // TDeskScrapView::CalcMinFrame:
- //----------------------------------------------------------------------------------------
- #pragma segment MAClipboard
-
- VRect TDeskScrapView::CalcMinFrame()// override
- {
- VRect minFrame(Inherited::CalcMinFrame());
-
- if (fDataHandle)
- if (fHaveText)
- {
- // TEXT
- if (this->Focus())
- {
- SetPortTextStyle(gSystemStyle);
-
- if (fSuperView)
- {
- minFrame = fSuperView->GetFrame();
- this->SuperToLocalVRect(minFrame);
- }
- CRect qdArea(ViewToQDRect(minFrame));
-
- if (GetHandleSize(fDataHandle) < 32000) // more than 32000 characters will kill TECalText
- {
- TEHandle aTEHandle = TENew(qdArea, qdArea);
- FailNIL((Ptr)aTEHandle);
-
- Handle oldHText = (*aTEHandle)->hText; // We'll need this for TEDispose
- (*aTEHandle)->hText = fDataHandle;
-
- TECalText(aTEHandle);
-
- minFrame.bottom = minFrame.top + Min((*aTEHandle)->nLines * (*aTEHandle)->lineHeight, kMaxCoord);
-
- (*aTEHandle)->hText = oldHText; // So TEDispose doesn't dispose our handle
-
- TEDispose(aTEHandle);
- }
- }
- }
- else
- {
- #if qDebugMsg
- if (gIntenseDebugging)
- {
- CRect picFrame = (**(PicHandle)fDataHandle).picFrame;
- fprintf(stderr, "Picture Frame:");
- fprintf(stderr, "%s\n", (const char *) picFrame);
- }
- #endif
- if (!gVarClipPicSize)
- {
- CRect picFrame = (*((PicHandle)fDataHandle))->picFrame;
- minFrame[botRight] = minFrame[topLeft] + VPoint(picFrame.GetSize());
- }
- }
-
- return minFrame;
- } // TDeskScrapView::CalcMinFrame
-
- //----------------------------------------------------------------------------------------
- // TDeskScrapView::CheckScrapContents:
- //----------------------------------------------------------------------------------------
- #pragma segment MAClipboard
-
- void TDeskScrapView::CheckScrapContents()
- {
- if (fScrapCount != gClipboardMgr->fNewScrapStuff.scrapCount)
- {
- // Make sure we get rid of the old scrap handle if necessary
- fDataHandle = DisposeIfHandle(fDataHandle);
-
- ResType resTypeFound;
- fHavePicture = LookForScrapType('PICT', resTypeFound);
- fHaveText = LookForScrapType('TEXT', resTypeFound);
-
- if (fHavePicture || fHaveText)
- {
- MAVolatileInit(Handle, aHandle, NULL);
- aHandle = NewPermHandle(0);
-
- FailInfo fi;
- Try(fi)
- {
- Boolean savedPerm = PermAllocation(TRUE);
- long offset;
- long length = GetScrap(aHandle, resTypeFound, &offset);
- savedPerm = PermAllocation(savedPerm);
-
- if (length < 0) // Only results < 0 are really errors
- FailOSErr((OSErr)length);
- fDataHandle = aHandle;
- this->AdjustFrame();
- this->Focus(); // Make sure we're still focused
- fi.Success();
- }
- else // Recover
- {
- aHandle = DisposeIfHandle(aHandle);
- fDataHandle = NULL;
- fi.ReSignal();
- }
- }
- }
- } // TDeskScrapView::CheckScrapContents
-
- //----------------------------------------------------------------------------------------
- // TDeskScrapView::Draw:
- //----------------------------------------------------------------------------------------
- #pragma segment MAClipboard
-
- void TDeskScrapView::Draw(const VRect& area)
- {
- MAVolatile(SignedByte, savedState);
- MAVolatileInit(Boolean, dataHandleIsLocked, FALSE);
-
- // Draw routine must not fail. Otherwise alert can cause an infinite loop of draw
- // fail, alert, update event, draw fail, etc..
- FailInfo fi;
- Try(fi)
- {
- #if qDebug
- this->AssumeFocused();
- #endif
-
- // Has scrap been updated since we last drew?
- this->CheckScrapContents();
-
- CRect aRect;
- if (fHaveText)
- {
- SetPortTextStyle(gApplicationStyle);
- aRect = this->GetQDExtent();
-
- dataHandleIsLocked = TRUE;
- savedState = LockHandle(fDataHandle);// Because MATextBox may move memory
-
- MATextBox(*fDataHandle, GetHandleSize(fDataHandle), aRect, teFlushDefault, kAutoWrap, NULL, kNoEraseFirst, kNoSpaceForCaret);
-
- HSetState(fDataHandle, savedState);
- dataHandleIsLocked = FALSE;
- }
- else if (fHavePicture)
- {
- if (gVarClipPicSize)
- aRect = this->GetQDExtent();
- else
- aRect = CRect(gZeroPt, ((CRect &) (**((PicHandle)fDataHandle)).picFrame).GetSize());
-
- DrawPicture((PicHandle)fDataHandle, &aRect);
- }
-
- if (fScrapCount != (gClipboardMgr->fNewScrapStuff.scrapCount))
- {
- this->ValidateRect(aRect); // avoid flicker upon clipboard installation
- fScrapCount = (gClipboardMgr->fNewScrapStuff.scrapCount);
- gClipboardMgr->fClipWrittenToDeskScrap = TRUE;// Don't need to write it back out.
- }
- fi.Success();
- }
- else // Recover
- {
- if (dataHandleIsLocked)
- {
- HSetState(fDataHandle, savedState);
- dataHandleIsLocked = FALSE;
- }
-
- // do nothing. We could draw a string "unable to show the clipboard at this time"
- // definitely *don't* resignal.
- }
-
- Inherited::Draw(area);
- } // TDeskScrapView::Draw
-
- //----------------------------------------------------------------------------------------
- // TDeskScrapView::SuperViewChangedFrame:
- //----------------------------------------------------------------------------------------
- #pragma segment MAClipboard
-
- void TDeskScrapView::SuperViewChangedFrame(const VRect& oldFrame,
- const VRect& newFrame,
- Boolean invalidate)// override
- {
- if (oldFrame.GetSize() != newFrame.GetSize())
- {
- this->AdjustFrame();
- if (invalidate)
- this->ForceRedraw(); // because of word wrap
- }
- } // TDeskScrapView::SuperViewChangedFrame
-
- //----------------------------------------------------------------------------------------
- // TDeskScrapView::WriteToDeskScrap:
- //----------------------------------------------------------------------------------------
- #pragma segment MAClipboard
-
- void TDeskScrapView::WriteToDeskScrap()// override
- {
- // This view represents data that's already written to the desk scrap
- } // TDeskScrapView::WriteToDeskScrap
-
- //----------------------------------------------------------------------------------------
- // LookForScrapType:
- //----------------------------------------------------------------------------------------
- #pragma segment MAClipboard
-
- Boolean TDeskScrapView::LookForScrapType(ResType theResType, ResType& resTypeFound)
- {
- long offset;
-
- long dataLength = GetScrap(NULL, theResType, &offset);
- if (dataLength > 0)
- resTypeFound = theResType;
- #if qDebugMsg
- if ((dataLength != noTypeErr) && (dataLength < 0))
- fprintf(stderr, "LookForScrapType: err = %d\n", dataLength);
- #endif
-
- return (dataLength > 0);
- } // LookForScrapType
-
-
- //----------------------------------------------------------------------------------------
- // End of UDeskScrapView.cp
-
- #pragma segment Inline
-